package com.cebbank.group.demoweb.controller;


import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.unit.DataSize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.MultipartConfigElement;

@RestController
@RequestMapping(value = "/upload")
@Configuration
public class UploadFileSizeController {

    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        //文件最大
        factory.setMaxFileSize(DataSize.ofKilobytes(1)); //KB,MB
        /// 设置总上传数据总大小
        factory.setMaxRequestSize(DataSize.ofKilobytes(10));
        return factory.createMultipartConfig();
    }

    @RequestMapping("/size")
    public String upload(@RequestParam("file") MultipartFile[] files){

        for (int i = 0; i < files.length; i++) {
            MultipartFile file = files[i];
            String name = file.getName();
            long size = file.getSize();
            String contentType = file.getContentType();
            System.out.println(String.format("%s, size : %s, type : %s",name,size,contentType));
        }
        return "success";
    }
}
